package com.good.shopping.product.service.impl;

import com.good.shopping.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.good.common.utils.PageUtils;
import com.good.common.utils.Query;

import com.good.shopping.product.dao.BrandDao;
import com.good.shopping.product.entity.BrandEntity;
import com.good.shopping.product.service.BrandService;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    // 品牌分类关联
    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    // 查询
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.eq("brand_id", key).or().like("name", key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params), wrapper
        );
        return new PageUtils(page);
    }

    /**
     * 修改
     *   更新品牌表，在其他关联表，有冗余字段，也需要更新
     */
    @Transactional
    @Override
    public void updateDetail(BrandEntity brand) {
        this.updateById(brand);
        if (!StringUtils.isEmpty(brand.getName())) {
            // TODO 更新其他关联

            // 更新品牌分类关联 的品牌字段
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());


        }
    }


    // in
    @Override
    public List<BrandEntity> getBrandsByIds(List<Long> brandIds) {
        return  baseMapper.selectList(new QueryWrapper<BrandEntity>().in("brand_id",brandIds));
    }

}
